package com.food.admin.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.food.admin.entity.FoodInfo;
import com.food.admin.entity.ProductsFoodRel;
import com.food.admin.entity.ProductsInfo;
import com.food.admin.entity.ProductsType;
import com.food.admin.exception.productsTypeException.ProductsTypeException;
import com.food.admin.exception.productsTypeException.ProductsTypeExceptionCode;
import com.food.admin.form.ProductsTypeAddForm;
import com.food.admin.form.ProductsTypeSearchForm;
import com.food.admin.form.ProductsTypeUpdateForm;
import com.food.admin.form.ProductsTypeUpdateStateForm;
import com.food.admin.mapper.FoodInfoMapper;
import com.food.admin.mapper.ProductsFoodRelMapper;
import com.food.admin.mapper.ProductsInfoMapper;
import com.food.admin.mapper.ProductsTypeMapper;
import com.food.admin.service.ProductsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.admin.util.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@Service
public class ProductsTypeServiceImpl extends ServiceImpl<ProductsTypeMapper, ProductsType> implements ProductsTypeService {
    @Autowired
    private ProductsTypeMapper productsTypeMapper;
    @Autowired
    private ProductsFoodRelMapper productsFoodRelMapper;
    @Autowired
    private ProductsInfoMapper productsInfoMapper;
    @Autowired
    private FoodInfoMapper foodInfoMapper;

    @Override
    public void addProductsType(ProductsTypeAddForm productsTypeAddForm) {
        //先查询数据库中是否已经建立过该类型
        QueryWrapper<ProductsType> productsTypeQueryWrapper = new QueryWrapper<>();
        productsTypeQueryWrapper.eq("type_name",productsTypeAddForm.getTypeName());
        ProductsType productsType = productsTypeMapper.selectOne(productsTypeQueryWrapper);
        //如果已经建立过该类型 抛出异常
        if(productsType!=null){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_EXIST);
        }
        //如果没有建立过该类型 则可以新增
        ProductsType pt = new ProductsType();
        pt.setTypeName(productsTypeAddForm.getTypeName());
        pt.setTypeState(1);//默认新增后该餐品类型是正常上架状态
        productsTypeMapper.insert(pt);
    }

    @Override
    public List<ProductsType> searchAllProductsType() {
        List<ProductsType> list = productsTypeMapper.selectList(null);
        if(list==null){//如果为空 抛异常 还未创建过餐品类型
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_NULL);
        }
        return list;
    }
    @Override
    public void updateProductsTypeName(ProductsTypeUpdateForm productsTypeUpdateForm) {
        //先查询是否存在该类别
        QueryWrapper<ProductsType> productsTypeQueryWrapper = new QueryWrapper<>();
        productsTypeQueryWrapper.eq("type_id",productsTypeUpdateForm.getTypeId());
        ProductsType productsType = productsTypeMapper.selectOne(productsTypeQueryWrapper);
        if(productsType==null){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_NOT_EXIST);
        }
        if(productsType.getTypeState()==0){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_DELETED);
        }
        //更新不能更新为已经存在了的类型
        QueryWrapper<ProductsType> wrapper = new QueryWrapper<>();
        wrapper.eq("type_name",productsTypeUpdateForm.getTypeName());
        ProductsType one = productsTypeMapper.selectOne(wrapper);
        if(one!=null){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_UPDATE_ERROR);
        }
        //更新菜品名称
        productsType.setTypeName(productsTypeUpdateForm.getTypeName());
        productsTypeMapper.updateById(productsType);
    }

    @Override
    public void updateProductsTypeState(ProductsTypeUpdateStateForm productsTypeUpdateStateForm) {
        //先判断传入的状态是否符合
        if(productsTypeUpdateStateForm.getTypeState()<0||productsTypeUpdateStateForm.getTypeState()>2){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_ERROR);
        }
        //先判断传入的typeid是否真实
        QueryWrapper<ProductsType> productsTypeQueryWrapper = new QueryWrapper<>();
        productsTypeQueryWrapper.eq("type_id",productsTypeUpdateStateForm.getTypeId());
        ProductsType productsType = productsTypeMapper.selectOne(productsTypeQueryWrapper);
        if(productsType==null){
            throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_NOT_EXIST);
        }
        //修改为上架状态
        if(productsTypeUpdateStateForm.getTypeState()==1){
            if(productsType.getTypeState()==1){
                throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_NORMAL);
            }
            if(productsType.getTypeState()==0){
                throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_DELETE);
            }
            //不管旗下是否有商品 都只更改类别状态为上架 由商家自己决定是否上架自己的菜品
            productsType.setTypeState(1);
            productsTypeMapper.updateById(productsType);

            //查询类别商家表
            QueryWrapper<ProductsInfo> productsInfoQueryWrapper = new QueryWrapper<>();
            productsInfoQueryWrapper.eq("products_type_id",productsTypeUpdateStateForm.getTypeId());
            List<ProductsInfo> productsInfos = productsInfoMapper.selectList(productsInfoQueryWrapper);
            if(productsInfos!=null){
                for (ProductsInfo productsInfo : productsInfos) {
                    productsInfo.setProductsState(1);
                    productsInfoMapper.updateById(productsInfo);
                }
            }
        }
        //修改为下架状态
        if(productsTypeUpdateStateForm.getTypeState()==2){
            if(productsType.getTypeState()==2){
                throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_TAKE_DOWN);
            }
            if(productsType.getTypeState()==0){
                throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_DELETE);
            }
            //查询旗下是否有商品
            QueryWrapper<ProductsFoodRel> productsFoodRelQueryWrapper = new QueryWrapper<>();
            productsFoodRelQueryWrapper.eq("products_id",productsTypeUpdateStateForm.getTypeId());
            if(productsFoodRelMapper.exists(productsFoodRelQueryWrapper)){//旗下有商品
                QueryWrapper<ProductsFoodRel> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("products_id",productsTypeUpdateStateForm.getTypeId());
                //查询餐品食品关联表
                List<ProductsFoodRel> list = productsFoodRelMapper.selectList(queryWrapper);
                for (ProductsFoodRel productsFoodRel : list) {//循环修改旗下食品状态为下架
                    productsFoodRel.setProductsFoodState(2);
                    productsFoodRelMapper.updateById(productsFoodRel);
                    QueryWrapper<FoodInfo> foodInfoQueryWrapper = new QueryWrapper<>();
                    foodInfoQueryWrapper.eq("food_info_id",productsFoodRel.getProductsFoodId());
                    FoodInfo foodInfo = foodInfoMapper.selectOne(foodInfoQueryWrapper);
                    foodInfo.setFoodInfoState(2);
                    foodInfoMapper.updateById(foodInfo);
                    productsType.setTypeState(2);
                    productsTypeMapper.updateById(productsType);
                }
            }else{//旗下没有商品
                productsType.setTypeState(2);
                productsTypeMapper.updateById(productsType);
            }
            //查询类别商家表
            QueryWrapper<ProductsInfo> productsInfoQueryWrapper = new QueryWrapper<>();
            productsInfoQueryWrapper.eq("products_type_id",productsTypeUpdateStateForm.getTypeId());
            List<ProductsInfo> productsInfos = productsInfoMapper.selectList(productsInfoQueryWrapper);
            if(productsInfos!=null){
                for (ProductsInfo productsInfo : productsInfos) {
                    productsInfo.setProductsState(2);
                    productsInfoMapper.updateById(productsInfo);
                }
            }
        }
        //修改为删除状态
        if(productsTypeUpdateStateForm.getTypeState()==0){
            if(productsType.getTypeState()==0){//已经是删除状态
                throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_DELETE);
            }
            if(productsType.getTypeState()==2){//已经是下架状态
                QueryWrapper<ProductsFoodRel> productsFoodRelQueryWrapper = new QueryWrapper<>();
                productsFoodRelQueryWrapper.eq("products_id",productsTypeUpdateStateForm.getTypeId());
                List<ProductsFoodRel> productsFoodRels = productsFoodRelMapper.selectList(productsFoodRelQueryWrapper);
                if(productsFoodRels.size()!=0){//如果类别下还有菜品 不能删除
                    throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_CANNOT_DELETE);
                }
                productsType.setTypeState(0);
                productsTypeMapper.deleteById(productsType);
               /* productsTypeMapper.updateById(productsType);*/
            }else if(productsType.getTypeState()==1) throw new ProductsTypeException(ProductsTypeExceptionCode.PRODUCTS_TYPE_EXCEPTION_NEED_TAKE_DOWN);
        }
    }
    @Override
    public MyPage<ProductsType> searchProductsType(ProductsTypeSearchForm productsTypeSearchForm) {
        QueryWrapper<ProductsType> queryWrapper = new QueryWrapper<>();
        if(productsTypeSearchForm.getTypeName()!=null&&productsTypeSearchForm.getTypeName()!=""){
            queryWrapper.like("type_name",productsTypeSearchForm.getTypeName());
        }
        if(productsTypeSearchForm.getTypeState()!=null){
            queryWrapper.eq("type_state",productsTypeSearchForm.getTypeState());
        }
        Page<ProductsType> page=new Page<ProductsType>(productsTypeSearchForm.getPageIndex()==null?1:productsTypeSearchForm.getPageIndex(),productsTypeSearchForm.getPageSize()==null?3:productsTypeSearchForm.getPageSize());
        productsTypeMapper.selectPage(page,queryWrapper);
        MyPage<ProductsType> myPage = new MyPage<ProductsType>();
        myPage.setPageIndex(page.getCurrent());
        myPage.setPageSize(page.getSize());
        myPage.setPages(page.getPages());
        myPage.setTotal(page.getTotal());
        myPage.setList(page.getRecords());
        return myPage;
    }
}
